package com.zhipu.resetbalance.ui.home.normal;

import androidx.databinding.ObservableField;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ConvertUtils;
import com.zhipu.resetbalance.common.Constants;
import com.zhipu.resetbalance.listener.SerialPortListener;
import com.zhipu.resetbalance.utils.popup.PopupUtils;
import com.zhipu.resetbalance.utils.serialPort.WeightSerialPortUtils;
import com.zhipu.resetbalance.utils.weight.WeightStringUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class NormalBalanceViewModel extends ViewModel  implements SerialPortListener {

    /**
     *
     */
    public ObservableField<String> WeighingWeight = new ObservableField<String>("0.00");


    /**
     * 定时器
     */
    private Disposable mDisposable;


    /**
     *
     */
    public void milliseconds(){
        /**
         * 设置回调
         */
        WeightSerialPortUtils.setSerialPortListener(this);
        /**
         *
         */
        mDisposable = Observable.interval(1, TimeUnit.MILLISECONDS).subscribe(number ->{
            /**
             * 重量輸出命令
             */
            WeightSerialPortUtils.WeightOutputCommand();
        });
    }

    /**
     * 释放定时器
     */
    public void ReleaseTimer(){
        if (mDisposable != null) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }


    /**
     * 功能\n天平
     */
    public void FunctionalBalance(){
        //功能\n天平
        ActivityUtils.finishActivity(NormalBalanceActivity.class);
    }


    @Override
    public void itemClicked(byte[] event) {
        //byte[]转字符串
        String numericalValue = ConvertUtils.bytes2HexString(event);
//        System.out.println(numericalValue);
        /**
         *
         */
        if(numericalValue.equals(Constants.Calibration100)){//標定100
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码100");
        }else if(numericalValue.equals(Constants.Calibration200)){//標定200
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码200");
        }else if(numericalValue.equals(Constants.Calibration500)){//標定500
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码500");
        }else if(numericalValue.equals(Constants.Calibration1000)){//標定1000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码1000");
        }else if(numericalValue.equals(Constants.Calibration2000)){//標定2000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码2000");
        }else if(numericalValue.equals(Constants.Calibration3000)){//標定3000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码3000");
        }else if(numericalValue.equals(Constants.SEND_GO_START)){//去皮完成
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("去皮成功");
        }else if(numericalValue.equals(Constants.overweight)){//超重
            WeighingWeight.set("超重");
        }else if(numericalValue.equals(Constants.SHIELD)){//校准中
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("校准中");
        }else if(numericalValue.equals(Constants.CalibrationComplete)){//校准完成
            WeighingWeight.set("校准完成");
        }else {
            String weightHandle = WeightStringUtils.weightHandle(event);
            /**
             * 打印重量
             */
            WeighingWeight.set(weightHandle);
        }
    }
}
